package com.bjfu.college.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjfu.college.dto.FilterCondition;
import com.bjfu.college.mapper.IntentionSchemeIntentionRecordMapper;
import com.bjfu.college.mapper.UniversityIntentionRecordMapper;
import com.bjfu.college.pojo.GlobalSetting;
import com.bjfu.college.pojo.UniversityIntentionRecord;
import com.bjfu.college.service.UniversityIntentionRecordService;
import com.bjfu.college.utils.ListUtils;
import com.bjfu.college.vo.IPageVO;
import com.bjfu.college.vo.RecordDetail;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 87290
* @description 针对表【university_intention_record】的数据库操作Service实现
* @createDate 2023-07-13 17:37:39
*/
@Service
public class UniversityIntentionRecordServiceImpl extends ServiceImpl<UniversityIntentionRecordMapper, UniversityIntentionRecord>
    implements UniversityIntentionRecordService{

    @Autowired
    UniversityIntentionRecordMapper universityIntentionRecordMapper;

    @Autowired
    IntentionSchemeIntentionRecordMapper intentionSchemeIntentionRecordMapper;

    /**
     * @Description: 通过条件筛选获取志愿记录
     * @Author: Pzf
     * @Date:   2023/7/16 11:08
     * @Param:  [filterCondition, studentProvince, studentScore, selectiveCombination]
     * @Return: java.util.List<com.bjfu.college.pojo.UniversityIntentionRecord>
     */
    @Override
    public List<UniversityIntentionRecord> getUniversityIntentionRecordsByCondition(FilterCondition filterCondition, String studentProvince, int studentScore, String selectiveCombination) {
        QueryWrapper<UniversityIntentionRecord> queryWrapper = new QueryWrapper<>();
        // 办学性质，省份，招生类别三个条件
        queryWrapper.in(ListUtils.listValidityJudge(filterCondition.getCollegeHoldCondition()),"school_property", filterCondition.getCollegeHoldCondition())
                .in(ListUtils.listValidityJudge(filterCondition.getProvinceCondition()),"province", filterCondition.getProvinceCondition())
                .in(ListUtils.listValidityJudge(filterCondition.getEnrollCategoryCondition()),"enrollment_category", filterCondition.getEnrollCategoryCondition());

        // 院校层次筛选条件
        List<String> collegeLevels = filterCondition.getCollegeLevelCondition();
        if (ListUtils.listValidityJudge(collegeLevels)){
            queryWrapper.nested(
                collegeLevelWrapper ->{
                    for (int i = 0; i < collegeLevels.size(); i++) {
                        if (i == collegeLevels.size() - 1){
                            collegeLevelWrapper.like("institutional_level",collegeLevels.get(i));
                        } else {
                            collegeLevelWrapper.like("institutional_level",collegeLevels.get(i)).or();
                        }
                    }
                }
            );
        }

        // 其他筛选条件处理
        if (ListUtils.listValidityJudge(filterCondition.getOtherCondition())){
            int size = filterCondition.getOtherCondition().size();
            for (int i = 0; i < size; i++) {
                String otherCondition = filterCondition.getOtherCondition().get(i);
                if (otherCondition.equals("只选大学")) queryWrapper.likeLeft("institution_name","大学");
                else if (otherCondition.equals("不选外省学院")) queryWrapper.nested(j -> j.ne("province", studentProvince).notLike("institution_name","学院"));
                else if (otherCondition.equals("招生人数大于1人")){
                    queryWrapper.gt("enrollment_plan",1);
                    break;
                } else if (otherCondition.equals("招生人数大于2人")){
                    queryWrapper.gt("enrollment_plan",2);
                    break;
                } else if (otherCondition.equals("招生人数大于3人")){
                    queryWrapper.gt("enrollment_plan",1);
                    break;
                }
            }
        }
        // 分数偏差处理
        int scoreBias = filterCondition.getScoreBias();
        queryWrapper.between("this_year_reference_score",studentScore - 2 * scoreBias, studentScore + scoreBias);
        // 招生专业条件处理
        Map<String, List<String>> majorCondition = filterCondition.getMajorCondition();
        List<String> majorClass = new ArrayList<>();
        for (String majorCategory : majorCondition.keySet()) {
            majorClass.addAll(majorCondition.get(majorCategory));
        }

        queryWrapper.in(ListUtils.listValidityJudge(majorClass),"major_class",majorClass);

        // 从表中获取组合能够满足的选科要求
        List<String> requirements = GlobalSetting.getInstance().getSubjectMap().get(selectiveCombination);
        queryWrapper.in(ListUtils.listValidityJudge(requirements),"subject_selection_requirements",requirements);
        queryWrapper.orderByDesc("this_year_reference_score");

        List<UniversityIntentionRecord> recordList = universityIntentionRecordMapper.selectList(queryWrapper);
        return recordList;
    }

    /**
     * @Description: 利用方案id查找该方案下的志愿记录
     * @Author: Pzf
     * @Date:   2023/7/16 14:06
     * @Param:  [schemeId, isAdjust]
     * @Return: java.util.List<com.bjfu.college.pojo.UniversityIntentionRecord>
     */
    @Override
    public IPageVO getUniversityIntentionRecordsById(int pageNum, int pageSize, int schemeId){
        // 创建pageInfo对象并设置页码和每页的大小
        PageInfo<RecordDetail> pageInfo;
        IPageVO iPageVO = new IPageVO();
        PageHelper.startPage(pageNum,pageSize);
        // 查询出recordDetails
        QueryWrapper<UniversityIntentionRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("scheme_id",schemeId).eq("is_deleted",0).orderByAsc("ranking");
        List<RecordDetail> RecordDetails = universityIntentionRecordMapper.getRecordsByJoin(queryWrapper);
        // 给pageInfo赋值
        pageInfo = new PageInfo<>(RecordDetails,pageSize);
        iPageVO.setList(pageInfo.getList());
        iPageVO.setCurrentPage(pageInfo.getPageNum());
        iPageVO.setTotal(pageInfo.getTotal());
        iPageVO.setPageSize(pageInfo.getSize());
        return iPageVO;

    }


    /**
     * @Description: 利用方案id获取被删除的志愿记录
     * @Author: Pzf
     * @Date:   2023/7/16 20:42
     * @Param:  [schemeId]
     * @Return: java.util.List<com.bjfu.college.pojo.UniversityIntentionRecord>
     */
    @Override
    public IPageVO getUniversityIntentionRecordsIsDeleted(int pageNum, int pageSize, int schemeId){
        // 创建pageInfo对象并设置页码和每页的大小
        PageInfo<RecordDetail> pageInfo;
        IPageVO iPageVO = new IPageVO();
        PageHelper.startPage(pageNum,pageSize);
        // 查询出被逻辑删除的中间表记录
        QueryWrapper<UniversityIntentionRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("scheme_id",schemeId);
        queryWrapper.eq("is_deleted",1);
        List<RecordDetail> recordDeletedDetails = universityIntentionRecordMapper.getRecordsByJoin(queryWrapper);
        // 给pageInfo赋值
        pageInfo = new PageInfo<>(recordDeletedDetails,pageSize);
        iPageVO.setList(pageInfo.getList());
        iPageVO.setCurrentPage(pageInfo.getPageNum());
        iPageVO.setTotal(pageInfo.getTotal());
        iPageVO.setPageSize(pageInfo.getSize());
        return iPageVO;
    }

    /**
     * @Description: 用方案id查找该方案下的志愿记录的Map形式
     * @Author: Pzf
     * @Date:   2023/7/16 23:08
     * @Param:  [schemeId]
     * @Return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    @Override
    public List<Map<String,Object>> getRecordsResultListMap(int schemeId){
        QueryWrapper<UniversityIntentionRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("scheme_id",schemeId).eq("is_deleted",0);
        return universityIntentionRecordMapper.getRecordsMapByJoin(queryWrapper);
    }

    /**
     * @Description: 获取所有的专业大类以及对应的专业小类
     * @Author: Pzf
     * @Date:   2023/7/22 15:46
     * @Param:  []
     * @Return: java.util.Map<java.lang.String,java.util.List<java.lang.String>>
     */
    @Override
    public Map<String,List<String>> getAllMajorCategoryAndClass(){
        Map<String,List<String>> allMajorCategoryAndClass = new HashMap<>();
        QueryWrapper<UniversityIntentionRecord> queryWrapper;
        List<String> majorCategory = universityIntentionRecordMapper.queryAllMajorCategory();
        for (String category : majorCategory) {
            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("major_category",category);
            List<String> majorClass = universityIntentionRecordMapper.queryAllMajorClassFromCategory(queryWrapper);
            allMajorCategoryAndClass.put(category,majorClass);
        }
        return allMajorCategoryAndClass;
    }
}




